<?php

namespace App\Api\Dao;

use App\Models\TongGood;

class GoodsDao extends BaseDao
{

    /**
     * 查询构造器
     * @param $data
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getBuildQuery($data): \Illuminate\Database\Eloquent\Builder
    {
        // TODO: Implement getBuildQuery() method.
        $query = TongGood::query();
        if(checkIsHasField($data,'id')){
            $query->where('id',$data['id']);
        }
        if(checkIsHasField($data,'goods_status')){
            $query->where('goods_status',$data['goods_status']);
        }
        if(checkIsHasField($data,'is_shop_goods')){
            $query->where('is_shop_goods',$data['is_shop_goods']);
        }
        if(checkIsHasField($data,'cate_id')){
            $query->where('cate_id',$data['cate_id']);
        }
        if(checkIsHasField($data,'goods_area')){
            $query->where('goods_area',$data['goods_area']);
        }
        return $query;
    }

    /**
     * 商品列表
     * @param array $param
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getGoodsList(array $param)
    {
        $param['goods_status'] = 1; //查询上架商品
        unset($param['is_shop_goods']);
        //dd($param);
        //dd($param);
        $query = $this->getBuildQuery($param);
        if(!checkIsHasField($param,'cate_id')){
            //$query->orWhere('is_shop_goods',2)->where('goods_status',1);
            $query->where('goods_status',1);
        }
        //echo $query->toSql();exit();
        return $query->paginate($param['size']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|void
     */
    public function getGoodsInfo($data)
    {

        return $this->getBuildQuery($data)->first();
    }


}
